package com.yxkj.syh.app.huarong.activities.user;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.DateUtils;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.yxkj.syh.app.huarong.bean.AppointmentResponse;
import com.yxkj.syh.app.huarong.bean.ReportResponse;
import com.yxkj.syh.app.huarong.bean.ReturnCarsResponse;
import com.yxkj.syh.app.huarong.bean.UserInfoResponse;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.data_center.model.AccountModel;
import com.yxkj.syh.app.huarong.data_center.model.ReportModel;
import com.yxkj.syh.app.huarong.util.UserManager;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/5/20 17:26
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class UserVM extends BaseViewModel<FragmentEvent> {
    public UserVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofAppointmentsSize = new ObservableField<>();
    public ObservableField<String> ofName = new ObservableField<>();
    public ObservableField<String> ofCount1 = new ObservableField<>();
    public ObservableField<String> ofCount2 = new ObservableField<>();
    public ObservableField<String> ofCount3 = new ObservableField<>();

    public MutableLiveData<AppointmentResponse> mldAppointmentsResponse = new MutableLiveData<>();

    public void userInfo(){
        AccountModel.getAccountModel().userInfo(mLifecycleProvider.bindToLifecycle(), new ApiObserver<UserInfoResponse>() {
            @Override
            public void onSuccess(UserInfoResponse userInfoResponse) {
                UserManager.getUserManager().saveUser(userInfoResponse.getData().getShiroUser());
                ofName.set(userInfoResponse.getData().getShiroUser().getRealName());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

    public void appointments() {

        ReportModel.getReportModel().appointments(mLifecycleProvider.bindToLifecycle(), new ApiObserver<AppointmentResponse>() {
            @Override
            public void onSuccess(AppointmentResponse response) {
                mldAppointmentsResponse.setValue(response);
                ofAppointmentsSize.set(response.getData().getRecords().size() + "");
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
                mldAppointmentsResponse.setValue(null);
            }
        });
    }

    public void report() {
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("start", "1990-01-01 00:00:00");
        pMap.put("end", DateUtils.getFormatTime(null, new Date()));
        ReportModel.getReportModel().report(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<ReportResponse>() {
            @Override
            public void onSuccess(ReportResponse reportResponse) {
                ReportResponse.Report report = reportResponse.getData();
                ofCount1.set(ComUtil.clearNoUseZero(report.getSuplierCount()).toPlainString());
                ofCount2.set(ComUtil.clearNoUseZero(report.getOrderCount()).toPlainString());
                ofCount3.set(ComUtil.clearNoUseZero(report.getOrderSales()).toPlainString());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }
}
